<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/mui.indexedlist.css" rel="stylesheet" />
		<style>
			html,
			body {
				height: 100%;
				overflow: hidden;
			}
			.mui-bar {
				-webkit-box-shadow: none;
				box-shadow: none;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">通讯录</h1>
		</header>
		<div class="mui-content">
			<div id='list' class="mui-indexed-list">
				<div class="mui-indexed-list-search mui-input-row mui-search">
					<input type="search" class="mui-input-clear mui-indexed-list-search-input" placeholder="搜索">
				</div>
				<div class="mui-indexed-list-bar">
					<a>A</a>
					<a>B</a>
					<a>C</a>
					<a>D</a>
					<a>E</a>
					<a>F</a>
					<a>G</a>
					<a>H</a>
					<a>I</a>
					<a>J</a>
					<a>K</a>
					<a>L</a>
					<a>M</a>
					<a>N</a>
					<a>O</a>
					<a>P</a>
					<a>Q</a>
					<a>R</a>
					<a>S</a>
					<a>T</a>
					<a>U</a>
					<a>V</a>
					<a>W</a>
					<a>X</a>
					<a>Y</a>
					<a>Z</a>
				</div>
				
				<div class="mui-indexed-list-alert"></div>
				<div class="mui-indexed-list-inner">
					<div class="mui-indexed-list-empty-alert">没有数据</div>
					<ul id="contactList" class="mui-table-view">
						<!-- <li data-group="A" class="mui-table-view-divider mui-indexed-list-group">A</li>
						<li class="mui-media mui-table-view-cell mui-indexed-list-item" style="padding: 10px 10px;">
							<img class="mui-media-object mui-pull-left" src="css/registLogin/arrow.jpg"
							style="max-width: 35px; height: 35px;">
							<div class="mui-media-body" style="line-height: 35px;">幸福</div>
						</li> -->					
					</ul>
				</div>
			</div>
		</div>
		<script src="js/mui.js"></script>
		<script src="js/mui.indexedlist.js"></script>
		<script src="js/app.js"></script>
		<script src="js/nickname.js"></script>
		<script type="text/javascript" charset="utf-8">
			mui.init();
			
			mui.plusReady(function(){
				
				//获取联系人,同时放到缓存中
				fetchMyFriends();
				
				//监听当前webview的show事件
				var thisWebview = plus.webview.currentWebview();
				thisWebview.addEventListener("show",function(){
					fetchMyFriends();
					// 从缓存中获取联系人列表，并且渲染到页面
					renderContactPage();
					//显示列表
					showList();
				});
				
			});
			
			// 26个字母外加 # 号
			var enWords = [
				'A', 'B', 'C', 'D', 'E', 'F', 'G', 
				'H', 'I', 'J', 'K', 'L', 'M', 'N',
				'O', 'P', 'Q', 'R', 'S', 'T', 
				'U', 'V', 'W', 'X', 'Y', 'Z',
				'#'
			];
			
			// 构建通讯录二维数组模型
			var contactArray = [
				[],[],[],[],[],[],[],[],[],
				[],[],[],[],[],[],[],[],[],
				[],[],[],[],[],[],[],[],[]
			];
			
			// 清空通讯录模型数组
			function clearContactArray() {
				contactArray = [
					[],[],[],[],[],[],[],[],[],
					[],[],[],[],[],[],[],[],[],
					[],[],[],[],[],[],[],[],[]
				]
			};
			
			
			function getOrderOfEnWords(char){
				//遍历获取位置
				for (var i = 0; i < enWords.length; i++) {
					if(enWords[i] == char){
						return i;
					}
				}
				//如果没有匹配到,则是其他数字,放置到#栏
				return enWords.length - 1 ;
				
			}
						
			//从缓存中获取联系人列表,渲染到页面
			function renderContactPage(){
				//1. 获取联系人列表
				var contactList = app.getContactList();
				//console.log(JSON.stringify(contactList));
				//2. 循环好友列表,将其转化为拼音
				for (var i = 0; i < contactList.length; i++) {
					var friend = contactList[i];
					//2.1 转换拼音
					var pinyin = words.convertPinyin(friend.friendNickname);
					//console.log(pinyin);
					//2.2 获取拼音的首字母
					var firstChar = pinyin.substr(0,1).toUpperCase();
					//console.log(firstChar);
					//2.3 获取在数组中的位置
					var order = getOrderOfEnWords(firstChar);
					//console.log(order);
					//2.4 获得顺序后,塞入对应的第一层模型
					contactArray[order].push(friend);
				}
				//console.log(JSON.stringify(contactArray));  
											
				//3. 构建通讯录html进行渲染
				var contactHtml = '';
				//先遍历生成A-#的标签
				for (var i = 0 ; i < contactArray.length ; i ++ ) {
					var friendArray = contactArray[i];
					if (friendArray.length > 0) {
						var nicknameStarter = enWords[i];
						//渲染第一层
						contactHtml += '<li data-group="' + nicknameStarter + '" class="mui-table-view-divider mui-indexed-list-group">' + nicknameStarter + '</li>';
						//渲染第二层
						for (var j = 0 ; j < friendArray.length ; j ++ ) {
							contactHtml += '' +
										'<li friendUserId="' + friendArray[j].friendId + '" friendNickname="' + friendArray[j].friendNickname + '" friendFaceImage="' + friendArray[j].friendFaceImage + '" class="chat-with-friend mui-media mui-table-view-cell mui-indexed-list-item" style="padding: 8px 10px;">' +
											'<img class="mui-media-object mui-pull-left" style="max-width: 35px;height: 35px;" src="' + app.imgServerUrl + friendArray[j].friendFaceImage + '"/>' +
											'<div class="mui-media-body" style="line-height: 35px;">' + friendArray[j].friendNickname + '</div>' +
										'</li>';
						}
					}
				}
				
				//console.log(contactHtml);
				
				// 渲染html
				document.getElementById("contactList").innerHTML = contactHtml;						
				
				// 清空数组						
				clearContactArray();
				
				//批量添加点击事件
				mui("#contactList").on("tap",".chat-with-friend",function(){
					//获取数据
					var friendUserId = this.getAttribute("friendUserId");
					//console.log("我查找的friendId时："+friendUserId);
					var friendNickname = this.getAttribute("friendNickname");
					var friendFaceImage = this.getAttribute("friendFaceImage");
					
					// 打开聊天子页面
					mui.openWindow({
						url: "chatting.html",
						id: "chatting-" + friendUserId,	// 每个朋友的聊天页面都是唯一对应的
						//传递数据到聊天页面
						extras: {
							friendUserId: friendUserId,
							friendNickname: friendNickname,
							friendFaceImage: friendFaceImage
						}
					});
				})
			}
								
			//获取联系人列表
			function fetchMyFriends(){
				var user = app.getUserGlobalInfo();
				//执行方法向后台获取数据, 获取我的朋友
				mui.ajax(app.serverUrl + "/user/queryMyFriends?userId=" + user.id,{
					data:{},
					dataType:'json',//服务器返回json格式数据
					type:'post',//HTTP请求类型
					timeout:10000,//超时时间设置为10秒；
					headers:{'Content-Type':'application/json'},	              
					success:function(data){
						if(data.status == 200){
							//console.log(JSON.stringify(data.data));
							app.setContactList(data.data);
						}
					}
				})
			}
			
			//显示通讯录
			function showList(){
				var list = document.getElementById('list');
				//calc hieght
				list.style.height = document.body.offsetHeight + 'px';
				//create
				window.indexedList = new mui.IndexedList(list);
			}
			
		</script>
	</body>

</html>
